<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/2/10
 * Time: 10:22
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\common\lib;

class Excel
{
    /**
     * 字母转数字
     * @param string $str
     * @return float|int
     */
    public static function excel2Int(string $str)
    {
        $num = 0;
        $strArr = str_split($str, 1);
        $lenght = count($strArr);
        foreach ($strArr as $k => $v) {
            $num += ((ord($v) - ord('A') + 1) * pow(26, $lenght - $k - 1));
        }
        return $num - 1;
    }

    /**
     * 数字转字母
     * @param $index
     * @param int $start
     * @return string
     */
    public static function IntToChr($index, $start = 65): string
    {
        $str = '';
        if (floor($index / 26) > 0) {
            $str .= self::IntToChr(floor($index / 26) - 1);
        }
        return $str . chr($index % 26 + $start);
    }
}
